package com.meihua.baoleyi.ui;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.meihua.baoleyi.R;
import com.meihua.baoleyi.base.RegBaseActivity;
import com.meihua.baoleyi.common.MyApp;
import com.meihua.baoleyi.dialog.PayPwdResetDialog;
import com.meihua.baoleyi.utils.MD5;
import com.meihua.baoleyi.utils.ToastUtil;
import com.meihua.baoleyi.utils.UrlContants;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by liming on 2017/10/17.
 * 账号与安全
 */

public class IdentitySafeActivity extends RegBaseActivity {

    @Bind(R.id.iv_setting_item)
    TextView ivSettingItem;
    @Bind(R.id.text_back)
    ImageView textBack;
    @Bind(R.id.text_title)
    TextView textTitle;

    @Bind(R.id.change_login_password)
    RelativeLayout changeLoginPassword;
    @Bind(R.id.find_pay_password)
    RelativeLayout findPayPassword;
    @Bind(R.id.set_pay_password)
    RelativeLayout setPayPassword;
    @Bind(R.id.name)
    TextView name;
    @Bind(R.id.phone)
    TextView phone;

    String mPhoneNum;

    @Override
    public int getLayoutResource() {
        return R.layout.activity_identitysafe;
    }

    @Override
    public void onInitView() {

    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_identitysafe);
        ButterKnife.bind(this);
        textTitle.setText("账号与安全");
        name.setText(MyApp.getInstance().getUname());//用户名
        phone.setText(MyApp.getInstance().getPhone().substring(0, 3) + "****" +MyApp.getInstance().getPhone().substring(7, 11));
    }

    @OnClick({R.id.text_back, R.id.change_login_password, R.id.find_pay_password, R.id.set_pay_password,R.id.rl_Phone,R.id.use_name})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.use_name:   //用户名点击
                String uname = MyApp.getInstance().getUname();
                startActivityForResult(ChangeUNameActivity.createIntent(IdentitySafeActivity.this, uname), 1);
                break;
            case R.id.text_back: //返回
                finish();
                break;
            case R.id.rl_Phone:  //手机号
                startActivityForResult(ResetPhoneActivity.createIntent(IdentitySafeActivity.this), 2);
                break;
            case R.id.change_login_password: //修改登入密码
                startActivity(new Intent(this, UpdateLoginPwdActivity.class));
                break;
            case R.id.find_pay_password:
                 SetPwd();//是否设置支付密码
                break;
            case R.id.set_pay_password:
                startActivity(new Intent(this, PayPwdActiity.class));
                break;
        }
    }

    /**
     * 设置支付密码
     */
    private void SetPwd() {
        long time=System.currentTimeMillis()/1000;//获取系统时间的10位的时间戳
        String str=String.valueOf(time);
        String token=myApp.getToken().toString();
        String sign_none="timestamp="+str+"&token="+token+"&user_id="+myApp.getUser_id();
        String sign_yes = MD5.getMessageDigest(sign_none.toString().getBytes()).toUpperCase();
        OkGo.get(UrlContants.HOST+"api/account/isSetPayPassword?timestamp="+str+"&token="+token+"&user_id="+myApp.getUser_id()+"&sign="+sign_yes)
                .tag(this)
                .params("timestamp",str)//时间戳
                .params("token",token)//token
                .params("user_id",myApp.getUser_id())
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        if (s != null) {
                            try {
                                JSONObject jsonObject = new JSONObject(s);
                                if (jsonObject.optString("code").equals("200")) {
                                    if (jsonObject.optString("is_set_pay").equals("1")) {    //未设置支付密码
                                        Toast.makeText(IdentitySafeActivity.this, jsonObject.optString("msg"), Toast.LENGTH_SHORT).show();
                                        startActivity(new Intent(IdentitySafeActivity.this,SetPayPwdActivity.class));
                                        finish();
                                    }else{
                                        AlreadyPwd();
                                    }
                                } else{
                                    Toast.makeText(IdentitySafeActivity.this, jsonObject.optString("msg"), Toast.LENGTH_SHORT).show();
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                        ToastUtil.showShort(IdentitySafeActivity.this, e.toString());
                    }
                });

    }

    private void AlreadyPwd(){
        mPhoneNum = myApp.getPhone().toString();
        mPhoneNum = mPhoneNum.replace(mPhoneNum.substring(3, 7), "****");
        PayPwdResetDialog.Builder builder = new PayPwdResetDialog.Builder(this);
        builder.setPhone(mPhoneNum);
        builder.setfanli("phone");
        builder.setPositiveButton("记得", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.dismiss();
                startActivity(new Intent(IdentitySafeActivity.this, FindPayPwdActivity.class));
            }
        });
        builder.setNativeButton("不记得", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.dismiss();
                startActivity(new Intent(IdentitySafeActivity.this, ResetPayPwdVerifyPhoneActivity.class));
            }
        });
        PayPwdResetDialog dlg = builder.build();
        dlg.show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == RESULT_OK) {
            String userName = data.getStringExtra("name");
            name.setText(userName);
        }
    }
}
